package io.renren.modules.gaokaobaobei.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.modules.gaokaobaobei.dao.AgencyDao;
import io.renren.modules.gaokaobaobei.entity.AgencyEntity;
import io.renren.modules.gaokaobaobei.entity.MyBaobeiEntity;
import io.renren.modules.gaokaobaobei.service.AgencyService;
import io.renren.modules.sys.entity.SysDeptEntity;
import io.renren.modules.sys.service.SysDeptService;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("agencyService")
public class AgencyServiceImpl extends ServiceImpl<AgencyDao, AgencyEntity> implements AgencyService {

    @Autowired
    AgencyDao agencyDao;
    @Autowired
    SysDeptService sysDeptService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String username = ShiroUtils.getSessionAttribute("username")+"";
        if(username.equals("admin")){
            username = null;
        }
        Integer total=agencyDao.getTotal(params);
        Integer limit=Integer.parseInt((String) params.get("limit"));
        Integer page=Integer.parseInt((String) params.get("page"));
        Integer pages=total/limit+1;
        params.put("start",(page-1)*limit);
        List<AgencyEntity> list=agencyDao.getList(params);

        PageUtils pageUtils=new PageUtils();
        pageUtils.setCurrPage(page);
        pageUtils.setList(list);
        pageUtils.setTotalCount(total);
        pageUtils.setPageSize(limit);
        pageUtils.setTotalPage(pages);
        return pageUtils;
    }

    @Override
    public int selectNumber(Map<String, String> params) {
        return baseMapper.selectNumber(params);
    }

    @Override
    public void updateNumber(Map<String, String> params) {
        baseMapper.updateNumber(params);
    }

    @Override
    public Long selectPersonNumber(Map<String, String> map) {
        return baseMapper.selectPersonNumber(map);
    }

    @Override
    public Object selectSum(Map<String, String> map) {
        return baseMapper.selectSum(map) ;
    }

    @Override
    public int userCount() {
        return baseMapper.userCount();
    }

    @Override
    public int performanceNumber(String type) {
        return baseMapper.performanceNumber(type);
    }

    @Override
    public int checkedNumber(String type) {
        return baseMapper.checkedNumber(type);
    }

    @Override
    public int userPerformanceNumber(Map map) {
        return baseMapper.userPerformanceNumber(map);
    }

    @Override
    public int userCheckedNumber(Map map) {
        return baseMapper.userCheckedNumber(map);
    }

    @Override
    public String dailiNumber(Map map) {
        return baseMapper.dailiNumber(map);
    }
    @Override
    public List countOutstanding(String username){
        List<Integer> countOutstanding = agencyDao.countOutstanding(username);
        return countOutstanding;
    }
    @Override
    public PageUtils getMyBaobeiList(Map<String, Object> params) {
        Integer total=agencyDao.getMyBaobeiTotal(params);
        Integer limit=Integer.parseInt((String) params.get("limit"));
        Integer page=Integer.parseInt((String) params.get("page"));
        Integer pages=total/limit+1;
        params.put("start",(page-1)*limit);
        List<MyBaobeiEntity> list=agencyDao.getMyBaobeiList(params);

        PageUtils pageUtils=new PageUtils();
        pageUtils.setCurrPage(page);
        pageUtils.setList(list);
        pageUtils.setTotalCount(total);
        pageUtils.setPageSize(limit);
        pageUtils.setTotalPage(pages);
        return pageUtils;
    }

    @Override
    public List<SysDeptEntity> getNewUserTree() {
        return agencyDao.getNewUserTree();
    }

    @Override
    public List<SysDeptEntity> getUserTree() {
        List<SysDeptEntity> list = sysDeptService.queryList(new HashMap<String, Object>());
        List<SysDeptEntity> userlist=agencyDao.getAllUser();
        for(SysDeptEntity sysDeptEntity:list){

            sysDeptEntity.setDeptId(1000000+sysDeptEntity.getDeptId());
            if(sysDeptEntity.getParentId()!=null)
            sysDeptEntity.setParentId(1000000+sysDeptEntity.getParentId());
        }
        for(SysDeptEntity sysDeptEntity:userlist){
            if(sysDeptEntity.getParentId()!=null)
            sysDeptEntity.setParentId(1000000+sysDeptEntity.getParentId());
            list.add(sysDeptEntity);
        }
        return list;
    }

    @Override
    public void updateDaili(AgencyEntity agency) {
        agencyDao.updateDaili(agency);
    }

}
